package com.wkk.config;


import com.wkk.interceptor.LoginInterceptor;
import com.wkk.listener.LoginListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //映射图片保存地址
        /*registry.addResourceHandler("/picture/**").
                addResourceLocations("file:D:\\IdeaItem\\fitnessManagement\\fitness\\src\\main\\resources\\static\\img\\");*/
        String path = "D:\\IdeaItem\\fitnessManagement\\fitness\\src\\main\\resources\\static\\images\\";
            registry.addResourceHandler("/courseUpload/**").
                    addResourceLocations("file:"+path);
        String path2 = "D:\\IdeaItem\\fitnessManagement\\fitness\\src\\main\\resources\\static\\img\\";
            registry.addResourceHandler("/upload/**").
                addResourceLocations("file:"+path2);

        WebMvcConfigurer.super.addResourceHandlers(registry);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        List<String> exclude = new ArrayList<>();
        //静态资源释放
        exclude.add("/css/**");
        exclude.add("/js/**");
        exclude.add("/img/**");
        exclude.add("/images/**");
        exclude.add("/lib/**");
        exclude.add("/assets/**");
        exclude.add("/data/**");

        exclude.add("/userLogin");
        exclude.add("/userRegister");
        exclude.add("/");
        exclude.add("/index");
        exclude.add("/login");
        exclude.add("/login-admin");
        exclude.add("/login-coach");
        exclude.add("/trainers");
        exclude.add("/classes");
        exclude.add("/about-us");
        exclude.add("/contact-us");
        exclude.add("/quit");
        registry.addInterceptor(loginInterceptor).addPathPatterns("/**").
                excludePathPatterns(exclude);
    }

    @Bean
    public ServletListenerRegistrationBean<LoginListener> servletListenerRegistrationBean(){
        return new ServletListenerRegistrationBean<>(new LoginListener());
    }
}
